<?php
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/11/7
 * Time: 1:18 PM
 */

namespace App\BO\OrderStatus\State;

use App\BO\OrderStatus\Events\OrderDistributedEvent;
use App\BO\OrderStatus\Events\OrderUserApplyRefundEvent;
use App\BO\OrderStatus\FlowTrait\AdminApplyRefundTrait;
use App\BO\OrderStatus\Meta\UserApplyRefundInfo;
use App\BO\OrderStatus\OrderStateContext;
use App\BO\OrderStatus\State\Refund\RefundWaitingAdminCheckState;

class DistributingState extends AOrderState
{
    use AdminApplyRefundTrait;

    protected $desc = '待分配';

    /**
     * @return bool
     */
    public function couldDistribute()
    {
        return true;
    }

    public function couldAdminApplyRefund()
    {
        return true;
    }

    /**
     * 分配操作
     * @return mixed|void
     * @throws \Exception
     */
    public function distribute()
    {
        $this->toWaitingService();
        event(new OrderDistributedEvent($this->getOrder()));  // 订单已经分配过了，待服务
    }

    /**
     * 用户申请退款操作
     * @param UserApplyRefundInfo $userApplyRefundInfo
     */
    public function applyRefund(UserApplyRefundInfo $userApplyRefundInfo)
    {
        $this->orderStateModifyStore->toRefundWaitingCheck();
        $this->stateContext->setOrderState(OrderStateContext::getInstance(RefundWaitingAdminCheckState::class));

        event(new OrderUserApplyRefundEvent($this->getOrder(), $userApplyRefundInfo));
    }
}
